import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';

const baseUrl = '/sales/api/v1';

const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '进销存单'
});

const getInitDesc = formatMessage({
    id: 'api.getInit',
    defaultMessage: '初始化'
});

const getListDesc = formatMessage({
    id: 'api.getList',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const getDetailDesc = formatMessage({
    id: 'api.getDetail',
    defaultMessage: '查询{resourceName}详情'
}, {resourceName});

const exportDesc = formatMessage({
    id: 'api.exportDesc',
    defaultMessage: '导出'
});

const getMarketDepartmentDesc = formatMessage({
    id: 'api.getMarketDepartmentDesc',
    defaultMessage: '查询大区'
});

const getBatchAbandon = formatMessage({
    id: 'api.getBatchAbandon',
    defaultMessage: '作废'
});

const getBatchEffective = formatMessage({
    id: 'api.getBatchEffective',
    defaultMessage: '生效'
});

const getBatchSalePlanOrder = formatMessage({
    id: 'api.getBatchSalePlanOrder',
    defaultMessage: '生成滚动计划'
});

const getAreasDesc = formatMessage({
    id: 'api.getAreasDesc',
    defaultMessage: '查询国家'
});
// 初始化请求
export const getInitData = () =>
    request(`${baseUrl}/ui/purchaseSaleInventoryOrders/init`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getInitDesc));

// 查询进销存单
export const getList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/purchaseSaleInventoryOrders?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询进销存单详情
export const getDetail = id => request(`${baseUrl}/purchaseSaleInventoryOrders/${id}`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));

// 导出
export const exports = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/purchaseSaleInventoryOrders/export?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc))
        .then(res => res.data);
};

// 查询大区
export const getMarketDepartment = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/marketDepartments?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getMarketDepartmentDesc));
};

// 快速查询大区
export const getMarketDepartmentSimple = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/marketDepartments/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getMarketDepartmentDesc));
};

//批量导入模板
export const ImportTemp = () => request(`${baseUrl}/purchaseSaleInventoryOrders/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, null)).then(res => res.data);

// 批量作废
export const batchAbandon = data => request(`${baseUrl}/purchaseSaleInventoryOrders/batchAbandon`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getBatchAbandon));

// 批量生效
export const batchEffective = data => request(`${baseUrl}/purchaseSaleInventoryOrders/batchEffective`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getBatchEffective));

// 批量生成滚动计划
export const batchSalePlanOrder = data => request(`${baseUrl}/purchaseSaleInventoryOrders/batchSalePlanOrder`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getBatchSalePlanOrder));

// 查询国家
export const getAreasSimple = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/areas/simple?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getAreasDesc));
};

// 快速查询国家
export const getAreas = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/areas/query?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getAreasDesc));
};
